package edu.cmu.cs211.proximitycounter.tests;

import edu.cmu.cs211.proximitycounter.counter.MyTreeSet;
import edu.cmu.cs211.proximitycounter.counter.Point;
import static org.junit.Assert.*;
import org.junit.Test;


public class SetTest {

	@Test
	public void rangeCountTest()
	{
		MyTreeSet<Integer> s = new MyTreeSet<Integer>();
		//Insert only the odd integers between 1 and 10
		for(int i=1; i< 10; i+=2)
		{
			s = s.insert(i);
		}

		MyTreeSet<Integer> w = new MyTreeSet<Integer>(s);
		assertEquals("s.rangeCount[2,4]", 1, (s.rangeCount(2,4)));
		assertEquals("s.rangeCount[0,-1]", 0, (s.rangeCount(0,-1)));
		assertEquals("s.rangeCount[4,4]", 0, (s.rangeCount(4,4)));
		assertEquals("s.rangeCount[1,9]", 5, (s.rangeCount(1,9)));	
		assertEquals("s.rangeCount[1,1]", 1, (s.rangeCount(1,1)));

		assertEquals("w.rangeCount[2,4]", 1, (w.rangeCount(2,4)));
		assertEquals("w.rangeCount[0,-1]", 0, (w.rangeCount(0,-1)));
		assertEquals("w.rangeCount[4,4]", 0, (w.rangeCount(4,4)));
		assertEquals("w.rangeCount[1,9]", 5, (w.rangeCount(1,9)));	

		MyTreeSet<Integer> set = new MyTreeSet<Integer>(3);
		assertEquals("set.rangeCount[2,4]", 1, (set.rangeCount(2,4)));
		assertEquals("set.rangeCount[0,-1]", 0, (set.rangeCount(0,-1)));
		assertEquals("set.rangeCount[4,4]", 0, (set.rangeCount(4,4)));
		assertEquals("set.rangeCount[1,9]", 1, (set.rangeCount(1,9)));	
	}
	
	@Test
	public void rangeCountConsecutiveElementsTest()
	{		
		MyTreeSet<Integer> set = new MyTreeSet<Integer>();
		for(int i = 1; i< 8; i++)
		{
			set = set.insert(i);
		}

		assertEquals("s.rangeCount[2,4]", 3, (set.rangeCount(2,4)));
		assertEquals("s.rangeCount[4,4]", 1, (set.rangeCount(4,4)));
		assertEquals("s.rangeCount[9,8]", 0, (set.rangeCount(9,8)));
		assertEquals("s.rangeCount[1,7]", 7, (set.rangeCount(1,7)));
		assertEquals("s.rangeCount[0,-1]", 0, (set.rangeCount(0,-1)));
		assertEquals("s.rangeCount[7,1]", 0, (set.rangeCount(7,1)));


		MyTreeSet<Integer> backwardsSet = new MyTreeSet<Integer>();
		for(int i=7; i > 0 ; i--)
		{
			backwardsSet = backwardsSet.insert(i);
		}
		assertEquals("backwardsSet.rangeCount[2,4]", 3, (backwardsSet.rangeCount(2,4)));
		assertEquals("backwardsSet.rangeCount[4,4]", 1, (backwardsSet.rangeCount(4,4)));
		assertEquals("backwardsSet.rangeCount[9,8]", 0, (backwardsSet.rangeCount(9,8)));
		assertEquals("backwardsSet.rangeCount[1,7]", 7, (backwardsSet.rangeCount(1,7)));
		assertEquals("backwardsSet.rangeCount[0,-1]", 0, (backwardsSet.rangeCount(0,-1)));
		assertEquals("backwardsSet.rangeCount[7,1]", 0, (backwardsSet.rangeCount(7,1)));
	}

	@Test
	public void rangeCountStressTest1()
	{		
		MyTreeSet<Integer> set = new MyTreeSet<Integer>();
		int n = 1000;
		for(int i=1; i< n; i++)
		{
			set = set.insert(i);
		}
		for(int a = 1; a < n; a++)
		{
			for(int b = a; b < n; b++)
			{
				assertEquals("set.rangeCount[a,b]", b+1-a, (set.rangeCount(a,b)));
			}
		}
	}

	}
